package com.powerunion.datacollection.report.excelreport.base.element;

import java.util.HashMap;
import java.util.Map;

import com.powerunion.datacollection.report.excelreport.Logger;
import com.powerunion.datacollection.report.excelreport.util.AnalyseTempletTool;
import com.powerunion.datacollection.report.excelreport.util.HelpTools;

/**
 * 元素属性对象,管理一个模板元素的属性集合
 * @author juny
 *
 */
public class Property {
	private static Logger log = Logger.getLogger(Property.class);
	
	public boolean setProperties(String properties){
		String[] pList = properties.split(SPLIT_PROPERTY_FLAG);
		String pName = null, pValue = null;
		if(null != pList){
			this.properties = new HashMap();
			for(int i=0; i<pList.length; i++){
				//取属性名称,取属性值
				pName = AnalyseTempletTool.getSubInfo(pList[i], 
							AnalyseTempletTool.REGEX_FIELD_PROPERTY_NAME);
				if(HelpTools.notEmpty(pName) != true){
					log.warn("Invalid property definition. [" + pList[i] + "]");
					continue;
				}
				pName = this.getTempletPropertyValue(pName);
				
				pValue = AnalyseTempletTool.getSubInfo(pList[i], 
						AnalyseTempletTool.REGEX_FIELD_PROPERTY_VALUE);
				if(HelpTools.notEmpty(pValue) != true){
					log.warn("Invalid property definition. [" + pList[i] + "]");
					continue;
				}
				pValue = this.getTempletPropertyValue(pValue);
				
				this.properties.put(pName, pValue);
			}
			return true;
		}
		return false;
	}
	
	public String getPropertyValue(String propertyName){
		if(null != this.properties){
			String sRet = (String)this.properties.get(propertyName);
			return sRet;
		}
		return "";
	}
	
	protected String getTempletPropertyValue(String pName){
		String value = AnalyseTempletTool.getSubInfo(pName, 
				AnalyseTempletTool.REGEX_FIELD_PROPERTY_GET_VALUE);
		if(HelpTools.notEmpty(value)){
			return value.substring(1, value.length()-1);
		}
		return "";
	}
	
	private static String SPLIT_PROPERTY_FLAG = ";";
	protected Map properties;
}
